package com.towker.core.encrypt;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 加密算法。
 * 现在只包含MD5算法。
 * @author zyg
 */
public class EncryptUtil
{
	/**
	 * 使用MD5加密
	 * @param inStr
	 * @return
	 * @throws Exception
	 */
	public static String encryptMd5(String inStr) throws Exception
	{
		MessageDigest md = null;
		String out = null;
		try
		{
			md = MessageDigest.getInstance("MD5");
			byte[] digest = md.digest(inStr.getBytes());
			out = byte2hex(digest);
		}
		catch (NoSuchAlgorithmException e)
		{
			e.printStackTrace();
			throw e;
		}
		return out;
	}

	private static String byte2hex(byte[] b)
	{
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++)
		{
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
			{
				hs = hs + "0" + stmp;
			}
			else
			{
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}
}
